package com.meme.editor.level;

import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.Color;
import java.util.List;
import java.util.Vector;
import java.util.ListIterator;

import org.apache.log4j.Logger;

import com.meme.editor.Box2d;
import com.meme.editor.EntityObject;
import com.meme.editor.PropertyValueModification;
import com.meme.editor.PropertyValueModificationList;
import com.meme.editor.level.define.DefineFile;
import com.meme.editor.model.sprite2d.AnimatedModule;
import com.meme.editor.model.sprite2d.AnimatedModuleGroup;
import com.meme.editor.property.Property;
import com.meme.editor.property.PropertyFactory;
import com.meme.io.editor.FileCache;
import com.meme.util.FileUtil;
import com.meme.util.MiscUtil;

public class Box2dAppearance extends Appearance 
{
	static Logger logger = Logger.getLogger(Box2dAppearance.class);

	
	public static final String ParamName_BorderColor = "border_color";
	public static final String ParamName_FilledColor = "filled_color";
	public static final String ParamName_Left = "left";
	public static final String ParamName_Top = "top";
	public static final String ParamName_Right = "right";
	public static final String ParamName_Bottom = "bottom";
	
	
	final static String GroupName_BoxGroup = "Box Properties";
	final static String PropertyName_Left        = "Left";
	final static String PropertyName_Right        = "Right";
	final static String PropertyName_Top        = "Top";
	final static String PropertyName_Bottom        = "Bottom";
	

	
	Color mBorderColor = new Color (0);
	Color mFilledColor = null;
	int mLeft = 0;
	int mTop = 0;
	int mRight = 0;
	int mBottom = 0;
	
	boolean mRelativeToActor = true;
	boolean mApplyActorScaleAndFlips = false;
	
	public Box2dAppearance ()
	{
		super ();
		
		createPropertyGroup (GroupName_BoxGroup);
		createPublicProperty (GroupName_BoxGroup, PropertyName_Left, PropertyFactory.ValueType_Number);
		createPublicProperty (GroupName_BoxGroup, PropertyName_Right, PropertyFactory.ValueType_Number);
		createPublicProperty (GroupName_BoxGroup, PropertyName_Top, PropertyFactory.ValueType_Number);
		createPublicProperty (GroupName_BoxGroup, PropertyName_Bottom, PropertyFactory.ValueType_Number);
	}
	
	@Override
	protected void _handleModifyingPropertyValues (PropertyValueModificationList modications)
	{			
		super._handleModifyingPropertyValues (modications);
		
		ListIterator<PropertyValueModification> iter = modications.listIterator();
		while ( iter.hasNext() )
		{
			PropertyValueModification mod = iter.next();
			
			if ( mod.mGroupName.equals (GroupName_BoxGroup) )
			{
				boolean handled = true;
				if ( mod.mPropertyName.equals (PropertyName_Left) )
				{
					setLeft (( (Float) (mod.mNewValue) ).intValue());
				}
				else if ( mod.mPropertyName.equals (PropertyName_Right) )
				{
					setRight (( (Float) (mod.mNewValue) ).intValue());
				}
				else if ( mod.mPropertyName.equals (PropertyName_Top) )
				{
					setTop (( (Float) (mod.mNewValue) ).intValue());
				}
				else if ( mod.mPropertyName.equals (PropertyName_Bottom) )
				{
					setBottom (( (Float) (mod.mNewValue) ).intValue());
				}
				else
					handled = false;
				
				if (handled)
					iter.remove();
			}
		}
	}	
	
	@Override
	public void setParameter (String paramName, String paramValue)
	{
		if (paramName.equals(ParamName_BorderColor))
		{
			mBorderColor = MiscUtil.parseColor(paramValue, 0x0);
		}
		else if (paramName.equals(ParamName_FilledColor))
		{
			mFilledColor = MiscUtil.parseColor(paramValue, 0x60ffffff);
		}
		else if (paramName.equals(ParamName_Left))
		{
			setLeft ( MiscUtil.parseInt(paramValue, 0x0) );
		}
		else if (paramName.equals(ParamName_Top))
		{
			setTop ( MiscUtil.parseInt(paramValue, 0x0) );
		}
		else if (paramName.equals(ParamName_Right))
		{
			setRight ( MiscUtil.parseInt(paramValue, 0x0) );
		}
		else if (paramName.equals(ParamName_Bottom))
		{
			setBottom ( MiscUtil.parseInt(paramValue, 0x0) );
		}
	}
	
	public void setLeft (int left)
	{
		mLeft = left;
		if (!sDisableValueValidating && mLeft > mRight)
			mLeft = mRight;
		changePublicPropertyValue (GroupName_BoxGroup, PropertyName_Left, mLeft);
	}
	
	public int getLeft ()
	{
		return mLeft;
	}
	
	public void setRight (int right)
	{
		mRight = right;
		if (!sDisableValueValidating && mRight < mLeft)
			mRight = mLeft;
		changePublicPropertyValue (GroupName_BoxGroup, PropertyName_Right, mRight);
	}
	
	public int getRight ()
	{
		return mRight;
	}

	public void setTop (int top)
	{
		mTop = top;
		if (!sDisableValueValidating && mTop > mBottom)
			mTop = mBottom;
		changePublicPropertyValue (GroupName_BoxGroup, PropertyName_Top, mTop);
	}
	
	public int getTop ()
	{
		return mTop;
	}
	
	public void setBottom (int bottom)
	{
		mBottom = bottom;
		if (!sDisableValueValidating && mBottom < mTop)
			mBottom = mTop;
		changePublicPropertyValue (GroupName_BoxGroup, PropertyName_Bottom, mBottom);
	}

	public int getBottom ()
	{
		return mBottom;
	}
	
	public void setRegionValues (int left, int top, int right, int bottom)
	{
		setLeft   (left);
		setTop    (top);
		setRight  (right);
		setBottom (bottom);
	}

//	@Override
//	public void show (Graphics2D g, int posX, int posY, boolean flipX, boolean flipY, int scale, int flag)
//	{
//		Color oldColor = g.getColor ();
//
//		if (mFilledColor != null)
//		{
//			g.setColor (mFilledColor);
//			g.fillRect(posX + mLeft, posY + mTop, mRight - mLeft, mBottom - mTop);
//		}
//		
//		g.setColor (mBorderColor);
//		g.drawRect(posX + mLeft, posY + mTop, mRight - mLeft, mBottom - mTop);
//		
//		g.setColor (oldColor);
//	}

	@Override
	public void show (Graphics2D g, int flag)
	{
		
		if (g == null)
			return;
		
		Color oldColor = g.getColor ();

		if (mFilledColor != null)
		{
			g.setColor (mFilledColor);
			g.fillRect(0 + mLeft, 0 + mTop, mRight - mLeft, mBottom - mTop);
		}
		
		g.setColor (mBorderColor);
		g.drawRect(0 + mLeft, 0 + mTop, mRight - mLeft, mBottom - mTop);
		
		g.setColor (oldColor);
	}

	@Override
	public void getBoundingBox (Box2d box2d)
	{
		box2d.set(mLeft, mTop, mRight, mBottom);
		
//		Actor actor = (Actor) getParent ();
//		if (mApplyActorScaleAndFlips)
//			box2d.adjust (actor.isFlipX (), actor.isFlipY ());
//		if (mRelativeToActor)
//			box2d.shift(actor.getPosX(), actor.getPosY());
	}
	
	@Override
	public boolean isGeometryAppearance ()
	{
		return true;
	}
}
